package com.example.socket.socket.simpledemo;

import java.io.*;
import java.net.Socket;

public class Client {
    public static void main(String[] args) {
        final String DEFULT_SERVER_HOST="127.0.0.1";
        final int DEFULT_SERVER_PORT=8888;
        Socket socket=null;
        BufferedWriter writer=null;
        //创建
        try{
            socket =new Socket(DEFULT_SERVER_HOST,DEFULT_SERVER_PORT);
            BufferedReader reader=new BufferedReader(new InputStreamReader(socket.getInputStream()));
             writer=new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
            //等待用户输入信息
            BufferedReader consoleReader=new BufferedReader(new InputStreamReader(System.in));
            String input = consoleReader.readLine();
            //发送信息给服务器
            writer.write(input+"\n");
            writer.flush();
            //读取服务器返回的消息
            String msg = reader.readLine();
            System.out.println(msg);

        }catch (Exception e){
            e.printStackTrace();
        }finally {
            if(writer==null){
              try{
                  writer.close();
                  System.out.println("关闭客户端的socket");
              }catch (Exception e){

              }
            }
        }

    }
}
